package com.tocchisu.tv.model;

import java.util.Date;

import org.joda.time.DateTime;

public class Slot extends Entity<Slot> implements Comparable<Slot> {
  private Hourly hourly;
  private Program program;
  private Integer audience;
  private DailySchedule dailySchedule;
  private DateTime startingDate;
  private DateTime endingDate;
  private News news;
  private Advertising ad;

  public Program getProgram() {
    return program;
  }

  public int compareTo(Slot program) {
    if (program == null) {
      return 1;
    }
    if (program.getHourly() == null)
      return 1;
    if (hourly == null)
      return -1;
    return this.getHourly().compareTo(program.getHourly());
  }

  @Override
  public boolean equals(Slot slot) {
    return this.getHourly().equals(slot.getHourly());
  }

  @Override
  public int hashCode() {
    return hourly.hashCode();
  }

  public Integer getAudience() {
    return audience;
  }

  public void setAudience(Integer audience) {
    this.audience = audience;
  }

  public void setProgram(Program program) {
    this.program = program;
  }

  public Hourly getHourly() {
    return hourly;
  }

  public DailySchedule getDailySchedule() {
    return dailySchedule;
  }

  public void setDailySchedule(DailySchedule dailySchedule) {
    this.dailySchedule = dailySchedule;
    updateDates();
  }

  public void setHourly(Hourly hourly) {
    this.hourly = hourly;
    updateDates();
  }

  private void updateDates() {
    if (dailySchedule == null || dailySchedule.getDate() == null || hourly == null)
      return;
    DateTime date = new DateTime(dailySchedule.getDate());
    date = date.withMillisOfDay(0);
    startingDate = date.plus(hourly.getPeriod());
    endingDate = startingDate.plus(hourly.getHourlyDuration());
  }

  public Date getStartingDate() {
    return startingDate.toDate();
  }

  public Date getEndingDate() {
    return endingDate.toDate();
  }

  public News getNews() {
    return news;
  }

  public void setNews(News news) {
    this.news = news;
  }

  public Advertising getAd() {
    return ad;
  }

  public void setAd(Advertising ad) {
    this.ad = ad;
  }
}